import React, { Component } from 'react'
import ReactDOM from 'react-dom';
import { Modal, Form, message } from 'antd';
import { fetchData } from '../../../components/AjaxConsumer'
import WorkAreaForm from './UpdateForm'

const confirm = Modal.confirm;

export default class UpdateWorkareaPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            formTitle: '',
            visible: false,
            resData: {},
            submitUrl: '/product/proCountingTask/c'
        };
    }

    //显示弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    // 隐藏弹出框
    hideModal = () => {
        this.setState({
            visible: false,
        });
    }
    // 新增工作区
    addWorkareaHandle = () => {
        this.setState(
            {
                submitUrl: '/product/proCountingTask/c',
                formTitle: '创建盘点任务',
                isEdit: false,
                resData: {}
            }
        );
        console.log("====11111111111111====");
        this.showModal();//显示弹框
    }

    // editGetInfoCallBack = (resData) => {
    //     console.log("修改数据：\n", JSON.stringify(resData, null, 4));
    //     this.setState(
    //         {
    //             submitUrl: '/wmsrest/material/odsRawUnitnit/u',
    //             resData: resData.data,
    //             isEdit: true,
    //             formTitle: '修改'
    //         }
    //     );
    //     this.showModal();//显示弹框
    // }

    // // 编辑信息
    // editHandle = (rowObj) => {
    //     this.setState({ resData: {} });
    //     //根据对象的Id 属性获取对象信息
    //     let odsRawUnitId = rowObj.odsRawUnitId;
    //     const options = {
    //         url: '/wmsrest/material/odsRawUnitnit/r/' + odsRawUnitId,
    //         type: 'get',//请求方式
    //         condition: { odsRawUnitId: odsRawUnitId }
    //     };
    //     console.log("====222222222222====");
    //     fetchData(this, options, this.editGetInfoCallBack);
    // }
    // 删除后的回调
    deleteCallBackHandle = (resData) => {
        let result = resData;
        //alert("提交返回数据：\n",JSON.stringify(result, null, 4));
        if (result.code == '1') {
            message.success('删除成功！');
            this.refreshHandle();
        } else {
            message.info(result.msg);
        }
    }

    // 删除工作区
    deleteHandle = (rowObj) => {
        if (rowObj.stauts != 0) {
            Modal.error({
                title: '错误提示',
                content: '该盘点任务正在进行或已经完成',
            });
            return;
        }
        let countingTaskId = rowObj.countingTaskId;
        let countingCode = rowObj.countingCode;
        let stauts = rowObj.stauts;
        const options = {
            url: '/product/proCountingTask/d',
            condition: {
                countingTaskId: countingTaskId,
                countingCode: countingCode,
                stauts: stauts
            }
        };
        //JSON.stringify(pstData),
        let _that = this;
        confirm({
            title: '您确定要删除吗？',
            onOk() {
                fetchData(_that, options, _that.deleteCallBackHandle);
                console.log("------------删除操作------------------")
            },
            onCancel() {
                console.log("取消了删除操作")
            },
        });
    }

    // 新增/修改后的回调
    handleOk = () => {
        this.hideModal();
        this.refreshHandle();
    }

    handleCancel = () => {
        //表单重置
        this.refs.modalFrom.resetFields();
        this.hideModal();
    }

    //刷新List列表
    refreshHandle = () => {
        console.log("刷新List列表")
        this.props.refreshHandle();
    }

    render() {
        if (this.state.resData != null && this.state.resData.data != null) {
            let factory = this.state.resData.data;
            this.getFormData.map((e, index) => {
                e.initialValue = factory[e.name]
            });
        }

        return (
            <Modal
                style={{ top: '20px' }}
                visible={this.state.visible}
                title={this.state.formTitle ? this.state.formTitle : '填写表单'}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width={this.formAlerSize == 'large' ? '850px' : '400px'}
                footer=''
            >

                <WorkAreaForm
                    ref="modalFrom"
                    isEdit={this.state.isEdit}
                    submitUrl={this.state.submitUrl}
                    onCancel={this.handleCancel}
                    factoryCode="all"
                    formData={this.state.resData}
                    refreshHandle={this.refreshHandle}
                />

            </Modal>
        )
    }
}